package com.work.xxzl.httpretrofit;

import java.io.File;
import java.util.List;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * author: Wangzhan
 * data :2018/3/10 0010 11:50
 * version:2.0.0
 * description:Retrofit2.0 获取MultiPart 用于上传文件工具类
 */

public class MultipartBodyUtils {

    /**
     * 单个文件
     */
    public static MultipartBody.Part getPart(String filepath) {
        File file = new File(filepath);//filePath 图片地址
        MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM);//表单类型
        RequestBody imageBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        builder.addFormDataPart("mp4", file.getName(), imageBody);//pic 后台接收图片流的参数名
        MultipartBody.Part part1 = builder.build().part(0);
        return part1;
    }

    /**
     * 单个文件
     */
    public static MultipartBody.Part getPartFile(File file) {
        MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM);//表单类型
        RequestBody imageBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        builder.addFormDataPart("mp4", file.getName(), imageBody);//pic 后台接收图片流的参数名
        MultipartBody.Part part1 = builder.build().part(0);
        return part1;
    }

    /**
     * 多个文件
     */
    public static List<MultipartBody.Part> getParts(List<String> pathList) {
        String token = "ASDDSKKK19990SDDDSS";//用户token
        MultipartBody.Builder builder = new MultipartBody.Builder()
                .setType(MultipartBody.FORM);//表单类型
//                .addFormDataPart(ParamKey.TOKEN, token);//ParamKey.TOKEN 自定义参数key常量类，即参数名
        //多张图片
        for (int i = 0; i < pathList.size(); i++) {
            File file = new File(pathList.get(i));//filePath 图片地址
            RequestBody imageBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
            builder.addFormDataPart("imgfile" + i, file.getName(), imageBody);//"imgfile"+i 后台接收图片流的参数名
        }
        List<MultipartBody.Part> parts = builder.build().parts();
        return parts;
    }
}
